/********************************************************************************/
/*  clickable.d																	*/
/*------------------------------------------------------------------------------*/
/*	製作		（　゜ワ゜）ノ　／　松久貫太									*/
/*	製作開始　	2007/12/06														*/
/*	MAIL		omega@personal.email.ne.jp										*/
/*	URL		http://nagoya.cool.ne.jp/o_mega										*/
/*																				*/
/*	このソースは「やわらかライセンス」の元で配布されています。					*/
/*-更新履歴---------------------------------------------------------------------*/
/*	2007/--/--																	*/
/*-その他-----------------------------------------------------------------------*/
/*	なし																		*/
/********************************************************************************/

/* ライブラリのインポート *******************************************************/
private import std.string;
private import vector;
private import hell2;

// koke依存拡張
class ImageButton{
	const int BUTTON_WIDTH = 80;
	const int BUTTON_HEIGHT = 32;
	
	int type;
	Vec3 position;
	
	enum TYPE{
		DEL,			// 削除
		HERBICIDE,		// 除草剤
		MANAGE_RAY,		// 運営レーザー
		CALL_NICO,		// ニコ厨を呼ぶ
		UP,				// うp
		FENCE,			// 柵
		
		ECONOMY,		// エコノミー
		CHART,			// チャート
		
		STOP,			// 停止
		PLAY,			// 通常
		KSK,			// 加速
		DANGER,			// 危
	}
	
	enum{
		LRG,	// 大
		SML,	// 小
		MIN,	// 極小
	}
	//						    del    he    ray   ni    up   fen   eco   crt   stp   ply   ksk   dgr
	const int type_width[]  = [  80 ,  80 ,  80 ,  80 ,  80 ,  80 ,  64 ,  64 ,  32 ,  32 ,  32 ,  32 ];
	const int type_height[] = [  32 ,  32 ,  32 ,  32 ,  32 ,  32 ,  16 ,  16 ,  16 ,  16 ,  16 ,  16 ];
	const int type_style[]  = [ LRG , LRG , LRG , LRG , LRG , LRG , SML , SML , MIN , MIN , MIN , MIN ];
	
	this(TYPE type){
		this.type = type;
	}
	
	void setPosition(Vec3 pos){
		position = pos;
	}
	
	int getWidth(){return type_width[type];}
	int getHeight(){return type_height[type];}
	
	void draw(bool selected,bool onMouse){
		if(type_style[type] == LRG){
			// イメージボタン大
			if(selected){			// 選択状態
				Hell_drawTexture("menu" , cast(int)position.x , cast(int)position.y
						, getWidth() * 2 , 0 , getWidth() , getHeight());
				Hell_drawTexture("menu" , cast(int)position.x , cast(int)position.y
						, 0 , (cast(int)type + 1) * getHeight()
						, getWidth() , getHeight()
						,1,1,0, 255,255,255);
			}else if(onMouse){		// onMouse状態
				Hell_drawTexture("menu" , cast(int)position.x , cast(int)position.y
						, getWidth() , 0 , getWidth() , getHeight());
				Hell_drawTexture("menu" , cast(int)position.x , cast(int)position.y
						, 0 , (cast(int)type + 1) * getHeight()
						, getWidth() , getHeight()
						,1,1,0, 64,64,64);
			}else{					// 他
				Hell_drawTexture("menu" , cast(int)position.x , cast(int)position.y
						, 0 , 0 , getWidth() , getHeight());
				Hell_drawTexture("menu" , cast(int)position.x , cast(int)position.y
						, 0 , (cast(int)type + 1) * getHeight()
						, getWidth() , getHeight()
						,1,1,0, 64,64,64);
			}
			
		}else if(type_style[type] == SML){
			// イメージボタン小
			int alpha = (onMouse || selected) ? 255 : 128;
			if(selected){			// 選択状態
				Hell_drawTexture("menu" , cast(int)position.x , cast(int)position.y
						, 242 , (cast(int)type - TYPE.ECONOMY) * getHeight()
						, getWidth() , getHeight()
						,1,1,0, 255,255,255,alpha);
			}else{					// 非選択状態
				Hell_drawTexture("menu" , cast(int)position.x , cast(int)position.y
						, 242 , (cast(int)type - TYPE.ECONOMY) * getHeight()
						, getWidth() , getHeight()
						,1,1,0, 128,128,128,alpha);
			}
		
		}else if(type_style[type] == MIN){
			// イメージボタン極小
			int alpha = (onMouse || selected) ? 255 : 128;
			if(selected){			// 選択状態
				Hell_drawTexture("menu" , cast(int)position.x , cast(int)position.y
						, 306 , (cast(int)type - TYPE.STOP) * getHeight()
						, getWidth() , getHeight()
						,1,1,0, 255,255,255,alpha);
			}else{					// 非選択状態
				Hell_drawTexture("menu" , cast(int)position.x , cast(int)position.y
						, 306 , (cast(int)type - TYPE.STOP) * getHeight()
						, getWidth() , getHeight()
						,1,1,0, 128,128,128,alpha);
			}
		}
	}
}

class ClickablePool{
	Clickable clist[];
	int count;
	
	bool none_enable;
	string selected;
	
	const int MAX_COUNT = 32;
	
	this(bool none_enable = true){
		clist = new Clickable[MAX_COUNT];
		foreach(inout click ; clist) click = null;
		
		this.count = count;
		this.selected = "";
		this.none_enable = none_enable;
	}
	
	void add(string str,Vec3 pos,Vec3 size){
		Clickable newc = new Clickable(str,pos,size);
		
		foreach(inout c ; clist){
			if(!c){
				c = newc;
				return;
			}
		}
	}
	void add(string str,Vec3 pos,ImageButton.TYPE type){
		ImageButton imgb = new ImageButton(type);
		Vec3 size = new Vec3(imgb.getWidth(), imgb.getHeight() , 0);
		Clickable newc = new Clickable(str , pos , size , 0 , imgb);
		
		foreach(inout c ; clist){
			if(!c){
				c = newc;
				if(selected == "" && none_enable == false) selected = str;
				return;
			}
		}
	}
	
	string getMouseOver(){
		foreach(inout c ; clist){
			if(c && c.onMouse()){
				return c.str;
			}
		}
		return "";
	}
	
	string getMouseClick(){
		if(Hell_isPushMouse()) {
			string mov = getMouseOver();
			if(mov != ""){
				if(selected == mov && none_enable == true){
					selected = "";
				}else{
					selected = mov;
				}
			}
			return mov;
		}
		return "";
	}
	
	string getSelected(){return selected;}
	
	void draw(){
		foreach(inout c ; clist){
			if(c){
				if(c.str == selected){
					c.selected = true;
				}else{
					c.selected = false;
				}
				c.draw();
			}
		}
	}
}


class Clickable{
	Vec3 position;
	Vec3 size;
	string str;
	float zoom;
	bool selected;
	
	ImageButton imagebutton;
	
	this(string str,Vec3 pos,Vec3 size,float zoom = 2.0,ImageButton imagebutton = null){
		this.str = str;
		this.position = pos;
		this.size = size;
		this.zoom = zoom;
		this.imagebutton = imagebutton;
		this.selected = false;
		
		if(imagebutton){
			imagebutton.setPosition(pos);
		}
	}
	
	bool onMouse(){
		if(position.x <= Hell_getMouseX() && position.x + size.x >= Hell_getMouseX()
			&& position.y <= Hell_getMouseY() && position.y + size.y >= Hell_getMouseY()) 
		{
			return true;
		}
		return false;
	}
	
	void draw(){
		if(imagebutton is null){
			// 通常
			if(onMouse()){
				Hell_drawFont(str , cast(int)position.x , cast(int)position.y , zoom , 255,128,32);
			}else{
				Hell_drawFont(str , cast(int)position.x , cast(int)position.y , zoom ,  64, 64, 64);
			}
		}else{
			imagebutton.draw(selected , onMouse());
		}
	}
}

/********************************************************************************/
